/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utt.edu.com.Controlador;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.swing.JOptionPane;
import utt.edu.com.Entidades.Perfil;

/**
 *
 * @author RODO IC
 */
public class CtrlPerfil extends CtrlConexionBaseDeDatos implements IGeneral {

    PreparedStatement objInsertar;
    ResultSet objConsulta = null;

    @Override
    public boolean insertar(Object _Ob) {
        Perfil p = (Perfil) _Ob;
        try {
            objInsertar = conectarBD().prepareStatement("agregarPerfil ?,?,?");
           objInsertar.setInt(1, p.getId());
            objInsertar.setString(2, p.getValor());
            objInsertar.setString(3, p.getDescripcion());

            objInsertar.execute();
            conectarBD().close();
            return true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
        }
        return false;
    }

    public boolean consulta(Object _Ob) {
        Perfil p = (Perfil) _Ob;
        try {
            objInsertar = conectarBD().prepareStatement("consultaPerfil ?");
            objInsertar.setInt(1, p.getId());

            objConsulta = objInsertar.executeQuery();

            while (objConsulta.next()) {
                p.setValor(objConsulta.getString(2));
                p.setDescripcion(objConsulta.getString(3));
            }

            objConsulta.close();
            conectarBD().close();
            return true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
        }
        return false;
    }

    @Override
    public boolean eliminar(Object _Ob) {
        Perfil p = (Perfil) _Ob;
        try {
            objInsertar = conectarBD().prepareStatement("eliminarPerfil ?");
            objInsertar.setInt(1, p.getId());
           objInsertar.execute();
            conectarBD().close();
            return true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
        }
        return false;
    }

    @Override
    public List<Object> consultar(Object _Ob) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean modificar(Object _Ob) {
        Perfil p = (Perfil) _Ob;
        try {
            objInsertar = conectarBD().prepareStatement("actualizarPerfil ?,?,?");
            objInsertar.setInt(1, p.getId());
            objInsertar.setString(2, p.getValor());
            objInsertar.setString(3, p.getDescripcion());

            objInsertar.execute();
            conectarBD().close();
            return true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
        }
        return false;
    }
}
